package com.baizhou.honglv.bean.exception;

import com.baizhou.honglv.bean.wrapper.ResultWrapper;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author HaiPeng Wang
 * @date 2021/7/17 16:25
 * @Description: 用于解决全局Exception
 */
@Component
@ControllerAdvice
/* @ControllerAdvice
    1、可以用于实现全局异常的处理
    2、全局数据的绑定 ModelAttibute
    3、全局数据预处理
                */
public class GlobalExceptionHandler {

    @ExceptionHandler
    @ResponseBody
    public ResultWrapper exceptionHandle(RuntimeException e){
        e.printStackTrace();
        return ResultWrapper.failure("系统错误");
    }

    @ExceptionHandler
    @ResponseBody
    public ResultWrapper exceptionHandle(SysException e){
        return ResultWrapper.failure(e.getMessage());
    }

    @ExceptionHandler
    @ResponseBody
    public ResultWrapper exceptionHandle(GlobalException e){
        return ResultWrapper.failure(e.getMessage());
    }
}
